/**
 * 
 */
package org.pump.client.codec;

import java.io.UnsupportedEncodingException;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.util.Arrays;

import org.apache.mina.core.buffer.IoBuffer;
import org.pump.client.type.NullTerminatedString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author liudong03
 *
 */
public class InitPack extends PacketBody{
	private static final Logger mylog = LoggerFactory.getLogger(Packet.class);
	
	byte protocol_version;
	NullTerminatedString server_version;
	long thread_id;
	byte[] scramble_buffer = new byte[8];
	byte filler1;
	byte[] server_capabilities = new byte[2];
	byte server_language;
	byte[] sever_status = new byte[2];
	byte[] filler2 = new byte[13];
	byte[] others = new byte[12];
	byte filler3;
	String salt;
	
	public static PacketBody readBuffer(IoBuffer in,int len)
	{
		if(in.remaining() < len)
		{
			return null;
		}
		int readlen = 0;
		InitPack initPack = new InitPack();
		initPack.protocol_version = in.get();
		readlen ++;
		initPack.server_version = NullTerminatedString.readBuffer(in, "ASCII");
		
		if(initPack.server_version == null)
		{
			mylog.error("read server version fail");
		}
		readlen += initPack.server_version.len;
		initPack.thread_id = in.getUnsignedInt();
		readlen += 4;
		in.get(initPack.scramble_buffer);
		readlen += initPack.scramble_buffer.length;
		initPack.filler1 = in.get();
		readlen ++;
		in.get(initPack.server_capabilities);
		readlen += initPack.server_capabilities.length;
		initPack.server_language = in.get();
		readlen ++;
		in.get(initPack.sever_status);
		readlen += initPack.sever_status.length;
		in.get(initPack.filler2);
		readlen += initPack.filler2.length;
		in.get(initPack.others);
		readlen += initPack.others.length;
		initPack.filler3 = in.get();
		readlen ++;
		
		if(readlen != len)
		{
			mylog.info("readlen is not equal len");
		}
		String salt1 = null;
		try {
			salt1 = new String(initPack.scramble_buffer,"US-ASCII");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			mylog.error("scrambe_buffer decode fail",e);
			return null;
		}
		String salt2 = null;
		try {
			salt2 = new String(initPack.others,"US-ASCII");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			mylog.error("others decode fail",e);
			return null;
		}
		initPack.salt = salt1 + salt2;
		
		return initPack;
	}

	/**
	 * @return the salt
	 */
	public String getSalt() {
		return salt;
	}


	public String getCharset()
	{
		String mycharset = null;
		switch(this.server_language)
		{
		case 28:
			mycharset = "gbk";
		}
		return mycharset;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "InitPack [protocol_version=" + protocol_version
				+ ", server_version=" + server_version + ", thread_id="
				+ thread_id + ", scramble_buffer="
				+ Arrays.toString(scramble_buffer) + ", filler1=" + filler1
				+ ", server_capabilities="
				+ Arrays.toString(server_capabilities) + ", server_language="
				+ server_language + ", sever_status="
				+ Arrays.toString(sever_status) + ", filler2="
				+ Arrays.toString(filler2) + ", others="
				+ Arrays.toString(others) + ", filler3=" + filler3 + ", salt="
				+ salt + "]";
	}
	
	

}
